#ifndef CHESSGUI_H
#define CHESSGUI_H

#include "model/ChessBoard.h"
#include "model/IBoardListener.h"
#include <QMainWindow>
#include <map>
#include <set>

class ChessGame;
namespace model
{
    class BoardEvent;
}

namespace view
{
    class ChessGUI : public QMainWindow
    {
        Q_OBJECT
        class Square;
        class ChangeListener;
    public:
        explicit ChessGUI(ChessGame&, model::ChessBoard&, QWidget* = 0);
        ~ChessGUI();

    protected:
        void mouseReleaseEvent(QMouseEvent *);

    private slots:
        void restart();

    private:
        ChessGame& ctrl;
        model::ChessBoard& board;
        typedef std::map<const model::Location, Square* const> vmap;
        const vmap grid;
        std::set<Square*> valids;
        Square* active;
        const model::IBoardListener* listener;

        void makeGrid();

        void makeMenu();

        void makeLabels();

        void reset();

        enum SquareState { DEFAULT, ACTIVE, HIGHLIGHT };

        class Square : public QWidget
        {
        public:
            const model::Location loc;

            Square(const model::Location&);

            SquareState getState() const;

            void setState(SquareState);

            void setPiece(const globals::cPiece&);

            const globals::cPiece& getPiece() const;
        protected:
            void paintEvent(QPaintEvent *);
        private:
            SquareState currState;
            globals::cPiece piece;
            const QImage* img;
            static const QImage act, capt, highl;
        };

        class ChangeListener : public model::IBoardListener
        {
        public:
            ChangeListener(ChessGUI* const);

            void BoardChanged(const model::BoardEvent) const;
        private:
            ChessGUI* const self;
        };

    };
}
#endif // CHESSGUI_H
